﻿using System;
using System.Drawing;
using System.ServiceModel.Security;
using System.Threading;
using System.Windows.Forms;
using BlueMarineWindowsFormsClient.AuthenticateService;

namespace BlueMarineWindowsFormsClient
{
    public partial class frmMain : Form
    {
        private static string uName = "";
        private static string pWord = "";
        private readonly Status _status;
        private string Role = "";

        private frmEmployeeManager emp;
        private frmHotelManager hotel;

        public frmMain(string uName, string pWord)
        {
            InitializeComponent();
            CenterToScreen();
            txtUserName.Text = uName;
            txtPassword.Text = pWord;
            Height = 225;
            panel2.Hide();
            CenterToScreen();
            _status = new Status(lblStatus);
        }

        public string UName
        {
            get { return uName; }
            set { uName = value; }
        }

        public string PWord
        {
            get { return pWord; }
            set { pWord = value; }
        }

        public frmHotelManager Hotel
        {
            get { return hotel; }
            set { hotel = value; }
        }

        public frmEmployeeManager Emp
        {
            get { return emp; }
            set { emp = value; }
        }


        private void btnSubmit_Click(object sender, EventArgs e)
        {
            submit();
        }

        private void submit()
        {
            try
            {
                var _service = new AuthenticateServiceClient("WSHttpBinding_IAuthenticateService")
                                   {
                                       ClientCredentials =
                                           {
                                               UserName = {UserName = txtUserName.Text, Password = txtPassword.Text},
                                               ServiceCertificate =
                                                   {
                                                       Authentication =
                                                           {
                                                               CertificateValidationMode =
                                                                   X509CertificateValidationMode.None
                                                           }
                                                   }
                                           }
                                   };
                Role = _service.RoleGet(txtUserName.Text, txtPassword.Text);


                uName = txtUserName.Text;
                pWord = txtPassword.Text;
                switch (Role)
                {
                    case "Recipient":
                        var bookingManagement = new frmBookingManagement(this);
                        bookingManagement.Show();
                        Hide();
                        break;

                    case "Manager":
                        panel1.Hide();
                        Height = 162;
                        statusStrip1.Hide();
                        panel2.Location = new Point(0, 0);
                        panel2.Show();
                        btnLogout.Enabled = true;
                        CenterToScreen();
                        break;
                    default:
                        _status.FAIL("Uncorrect UserName, Password");
                        txtPassword.Text = "";
                        break;
                }
            }
            catch (Exception ex)
            {
                _status.ERROR(ex.Message);
            }
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
        }

        private void chbxChangePassWord_CheckedChanged(object sender, EventArgs e)
        {
            if (!chbxChangePassWord.Checked) return;
            uName = txtUserName.Text;
            pWord = txtPassword.Text;
            var t = new Thread(ThreadProc);
            t.Start();
            Close();
        }

        public static void ThreadProc()
        {
            Application.Run(new frmChangePassword(uName, pWord));
        }

        private void btnLogout_Click(object sender, EventArgs e)
        {
            uName = "";
            pWord = "";
            var t = new Thread(ThreadProc2);
            t.Start();
            Close();
        }

        public static void ThreadProc2()
        {
            Application.Run(new frmMain(uName, pWord));
        }

        private void btnEmpMan_Click(object sender, EventArgs e)
        {
            emp = new frmEmployeeManager(this);
            emp.Show();
            Hide();
        }

        private void btnHotelMan_Click(object sender, EventArgs e)
        {
            hotel = new frmHotelManager(this);
            hotel.Show();
            Hide();
        }

        private void txtUserName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                submit();
            }
        }
    }
}